package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.*;
import cn.com.connext.scrm.repository.master.*;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.mapper.ClickCountService;
import cn.com.connext.scrm.utils.wdtutils.StringUtils;
import cn.com.connext.scrm.web.rest.vm.ScanCodeRecordVM;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class ScanCodeRecordImpl {
    @Autowired
    private ScanCodeRecordRepository scanCodeRecordRepository;
    @Autowired
    private ZnhjRepository znhjRepository;

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private MemberRepository memberRepository;


    public JsonResult addScanCodeMsg(ScanCodeRecordVM vm) {
        JsonResult result = new JsonResult();
        if (vm != null) {
            if (StringUtils.isEmpty(vm.getSkuId())) {
                result.setState(-1);
                result.setMessage("skuId不能为空！");
            } else {
                ScanCodeRecord record = new ScanCodeRecord();
                BeanUtils.copyProperties(vm, record);
                record.setCreateTime(ZonedDateTime.now());
                scanCodeRecordRepository.save(record);
                result.setState(0);
                result.setMessage("添加成功");
            }
        }
        return result;
    }

    public JsonResult statisticsScanCount(ScanCodeRecordVM vm) {
        JsonResult result = new JsonResult();
        if (StringUtils.isEmpty(vm.getSkuId())) {
            result.setState(-1);
            result.setMessage("skuId不能为空！");
        } else {
            //统计次数brandPage
            Map<String, Map<String, Long>> map = new HashMap<>();
            List<ScanCodeRecord> list = scanCodeRecordRepository.findAll();
            if (list != null && list.size() > 0) {
                Map<String, Long> map1 = list.stream().collect(Collectors.groupingBy(ScanCodeRecord::getSkuId, Collectors.counting()));
                result.setData(map1);
                map.put("allCount", map1);
            }
            //统计注册人数
            List<Member> list2 = memberRepository.findAll();
            if (list2 != null && list2.size() > 0) {
                Map<String, Long> map2 = list2.stream().collect(Collectors.groupingBy(Member::getSkuId, Collectors.counting()));
                map.put("allPeopleCount", map2);
            }
            //购买且扫描数 (被扫描的码数)
            List<ZnhjEntity> list3 = znhjRepository.findIsSanCode();
            if (list3 != null && list3.size() > 0) {
                Map<String, Long> map3 = list3.stream().collect(Collectors.groupingBy(ZnhjEntity::getSkuId, Collectors.counting()));
                map.put("buyAndScanCount", map3);
            }
            //积分兑换数(兑换人数)
            List<OrderRecord> list4 = orderRepository.findOrderMsgBySkuId();
            if (list4 != null && list4.size() > 0) {
                Map<String, Long> map4 = list4.stream().collect(Collectors.groupingBy(OrderRecord::getSkuId, Collectors.counting()));
                map.put("pointConverCount", map4);
            }
            result.setData(map);
        }
        return result;
    }
}

